package com.wty.love.smallGames;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.wty.love.R;

public class WebActivity extends AppCompatActivity {
    public static String URL = "URL";
    public static String flightChess = "http://00houqq.cn/fxq/";
    public static String keyTone = "http://00houqq.cn/musicplay/";
    public static String url2048 = "http://00houqq.cn/2048/";
    public static String othersGames = "http://m.g112.com/?00houqq.cn";
    public static String eatFlies = "http://00houqq.cn/qingwa/";
    public static String url520 = "http://qgapk.top/520/love.html?me=%E6%96%87%E7%AB%A0%E5%AE%87&you=%E7%90%AA%E7%90%AA&yi=%E6%89%BF%E8%92%99%E4%BD%A0%E7%9A%84%E5%87%BA%E7%8E%B0%EF%BC%8C%E4%BD%A0%E6%98%AF%E6%88%91%E6%8B%94%E6%8E%89%E6%B0%A7%E6%B0%94%E7%BD%90%E9%83%BD%E6%83%B3%E5%90%BB%E7%9A%84%E4%BA%BA%EF%BC%8C%E4%BD%A0%E6%98%AF%E6%88%91%E8%B7%91%E5%AE%8C8000%E7%B1%B3%E8%BF%98%E6%83%B3%E6%8B%A5%E6%8A%B1%E7%9A%84%E4%BA%BA%E3%80%82%E4%BD%A0%E6%98%AF%E6%88%91%E7%94%B5%E9%87%8F%E5%8F%AA%E5%89%A91%EF%BC%85%E4%B9%9F%E6%83%B3%E5%9B%9E%E4%BF%A1%E6%81%AF%E7%9A%84%E4%BA%BA&wu=%E5%8E%8B%E5%8A%9B%E5%A4%A7%E5%BF%83%E6%83%85%E4%B8%8D%E5%A5%BD%E5%B0%B1%E6%89%BE%E6%88%91%E5%91%80%EF%BC%8C%E6%88%91%E6%9D%A5%E9%80%97%E4%BD%A0%E5%BC%80%E5%BF%83&year=2021&yue=3&ri=26";
    public static String rotateSix = "http://00houqq.cn/xuanzhuanliubianxing/";
    public static String noKill = "http://00houqq.cn/yigedoubunengsi/";
    public static String cutBlocks = "http://00houqq.cn/qiejimu/";
    public static String pin = "http://00houqq.cn/jianfengchazhen/";
    public static String mf = "http://00houqq.cn/mf/";
    public static String blackAndWhite = "http://00houqq.cn/heibaiqi/";

    private WebView webView;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        initView();
        initData();
    }

    private void initView(){
        webView = findViewById(R.id.webView);
        title = findViewById(R.id.title);
    }

    private void initData(){
        String url = getIntent().getStringExtra(URL);
        System.out.println("url:"+url);
        if (url.equals(flightChess)) {
            title.setText("飞行棋");
            findViewById(R.id.bottom).setVisibility(View.VISIBLE);
//            Uri uri = Uri.parse(flightChess);
//            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//            startActivity(intent);
        }else if (url.equals(url2048)){
            title.setText("2048");
            Toast.makeText(this,"请在格子内滑动~❤",Toast.LENGTH_SHORT).show();
        }else if (url.equals(keyTone)){
            title.setText("有趣的按键音");
        }else if (url.equals(eatFlies)){
            title.setText("喂青蛙");
        }else if (url.equals(othersGames)){
            title.setText("在线小游戏");
        }else if (url.equals(rotateSix)){
            title.setText("旋转六边形");
        }else if (url.equals(noKill)){
//            findViewById(R.id.bottom).setVisibility(View.VISIBLE);
            title.setText("一个都不能死");
        }else if (url.equals(cutBlocks)){
            title.setText("切方块");
        }else if (url.equals(pin)){
            title.setText("见缝插针");
        }else if (url.equals(mf)){
            title.setText("在线魔方");
        }else if (url.equals(blackAndWhite)){
//            findViewById(R.id.bottom).setVisibility(View.VISIBLE);
            title.setVisibility(View.GONE);
            title.setText("黑白棋");
        }

        //缩放开关，设置此属性，仅支持双击缩放，不支持触摸缩放
        webView.getSettings().setSupportZoom(true);
        //设置是否可缩放，会出现缩放工具（若为true则上面的设值也默认为true）
        webView.getSettings().setBuiltInZoomControls(true);
        //隐藏缩放工具
        webView.getSettings().setDisplayZoomControls(false);
        // 开启 localStorage
        webView.getSettings().setDomStorageEnabled(true);
        // 设置支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 启动缓存
        webView.getSettings().setAppCacheEnabled(true);
        // 设置缓存模式
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        //使用自定义的WebViewClient
        webView.setWebViewClient(new WebViewClient()
        {
            //覆盖shouldOverrideUrlLoading 方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl(url);
    }

    // 覆盖onKeydown 添加处理WebView 界面内返回事件处理
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack())
        {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}